home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6732 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  49 lines

  1. Path: newsserver.rdcs.kodak.com!fietze
  2. From: fietze@kagcpd01.ag01.kodak.COM (Roman Fietze)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Bug or Feature? const int variables do not export in gcc 2.7.0
  5. Followup-To: comp.lang.c++
  6. Date: 12 Feb 1996 07:41:02 GMT
  7. Organization: Eastman Kodak Company
  8. Message-ID: <FIETZE.96Feb12084102@kagcpd01.ag01.kodak.COM>
  9. References: <4fdlee$bpf@netlab.cs.rpi.edu>
  10. NNTP-Posting-Host: kagcpd01.ag01.kodak.com
  11. In-reply-to: ivo.welch@AGSM.UCLA.EDU's message of 8 Feb 1996 20:11:58 -0000
  12.  
  13. In article <4fdlee$bpf@netlab.cs.rpi.edu> ivo.welch@AGSM.UCLA.EDU (Ivo Welch) writes:
  14.  
  15.    On a NextStep/FIP system w/ gcc 2.7.0:
  16.  
  17.    File try2.cc:
  18.        int ex1=2;
  19.  
  20.    File try1.cc:
  21.        extern "C" { 
  22.        #include <stdio.h>
  23.        }
  24.  
  25.        int main() {
  26.          extern int ex1; printf("Extern = %d\n", ex1); return 0; }
  27.  
  28.    works, but if I replace "int" with "const int" in both situations, the 
  29.    linker complains that ex1 is not defined.  NOTE that it works with "const" 
  30.    in gcc if instead of C++ I switch to plain C.
  31.  
  32. It looks funny, but you have to say:
  33.  
  34. extern const int ex1 = 2;    // where you define it
  35.  
  36. and
  37.  
  38. extern const int ex1;        // where else you need it.
  39.  
  40. const's are local to theit translation units by default, you have to
  41. declare them extern if you need them outside a module (see
  42. Ellis/Stroustrup, The Annotated C++ Ref. Manula, 3.3 Program and
  43. Linkage).
  44.  
  45. Roman
  46. -- 
  47. Roman Fietze (Mail Code 5023)              Kodak AG Stuttgart/Germany
  48. fietze@kagcpd01.ag01.kodak.COM                       fietze@kodak.COM
  49.